package com.littlebee.storysuper.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.littlebee.storysuper.R;
import com.littlebee.storysuper.entry.NewstVideoEntry;
import com.littlebee.storysuper.entry.User;
import com.littlebee.storysuper.utils.DateUtils;
import com.littlebee.storysuper.utils.GsonUtils;
import com.littlebee.storysuper.utils.NetWorkUtils;
import com.littlebee.storysuper.utils.Utils;
import com.zss.library.activity.BaseActivity;
import com.zss.library.adapter.CommonAdapter;
import com.zss.library.adapter.ViewHolder;
import com.zss.library.toolbar.CommonToolbar;
import com.zss.library.utils.LogUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 视频列表
 */
public class VideoListActivity extends BaseActivity implements View.OnClickListener {

    private ListView mListView;
    CommonAdapter<NewstVideoEntry> mAdapter;
    List<NewstVideoEntry> entries=new ArrayList<NewstVideoEntry>();

    @Override
    public int getLayoutResId() {
        return R.layout.activity_today_story;
    }

    @Override
    public void initView() {
        super.initView();
        mListView = (ListView) findViewById(R.id.listview);

    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);

        loadData();
        mAdapter = new CommonAdapter<NewstVideoEntry>(getActivity(), R.layout.layout_item_video) {
            @Override
            protected void convert(ViewHolder viewHolder, final NewstVideoEntry workerListEntry, int i) {
                TextView name = viewHolder.findViewById(R.id.video_title_tv);
                ImageView bg = viewHolder.findViewById(R.id.init_video_iv);
                ImageView play = viewHolder.findViewById(R.id.show_to_player);
                name.setText(workerListEntry.videostartday);
                Glide.with(VideoListActivity.this).load(workerListEntry.videominiphoto).error(R.color.module_video_bg).into(bg);

            }
        };
        mListView.setAdapter(mAdapter);
        mAdapter.addAll(entries);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(VideoListActivity.this,PlayerInfoActivity.class);
                intent.putExtra("url",entries.get(position).videourl);
                intent.putExtra("title",entries.get(position).videostartday);
                intent.putExtra("pos",0);
                intent.putExtra("pic",entries.get(position).videominiphoto);
                startActivity(intent);
            }
        });

    }

    @Override
    public void setTopBar() {
        super.setTopBar();
        setStatusBarColor(R.color.module_video_bg);
        CommonToolbar toolbar = getToolbar();
        toolbar.setBgRes(R.color.module_video_bg);
        toolbar.setTitle("视频列表");

    }

    /*
   * 今日推荐故事
   *
   * */
    public void loadData() {
        Map<String, String> hashMap = new HashMap<>();
        User user = Utils.getUserEntry();
        hashMap.put("phoneId", user.getMobile());
        long times = System.currentTimeMillis();
        hashMap.put("startdate", "2017-06-30");
        hashMap.put("enddate", DateUtils.getTimeDay1(times));

        NetWorkUtils.post(getActivity(), "getbabyhistoryvideo", hashMap, true, new NetWorkUtils.IListener() {

            @Override
            public void onSuccess(String result, JSONObject resObj) {
                String res = resObj.optString("response")/**.optString("storypages")*/;
                entries = GsonUtils.getListFromJSON(res, NewstVideoEntry.class);
                mAdapter.replaceAll(entries);
            }

            @Override
            public void onError(String result, String code, String msg) {
                LogUtils.i("XHX", "XHXERROR:" + result);


            }
        });
    }



    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
